import { cloneDeep } from 'lodash'
import { $http } from '../utils/axios'
import router from '../router/index'
export default {
  name: 'sensorStore',
  state: {
    downloadId: '',
    sensorList: [],
    sensorCheckList: []
  },
  getters: {
    sensorList: (state) => state.sensorList,
    sensorListFilter: (state) => {
      return state.sensorList.map(item => {
        const node = cloneDeep(item)
        if (state.sensorCheckList.includes(item.cdCode)) {
          node.disabled = true
        }
        return node
      })
    }
  },
  mutations: {
    SET_SENSOR_LIST (state, value) {
      state.sensorList = value
      localStorage.setItem('sensorList', JSON.stringify(value))
    },
    SET_SENSOR_CHECK_LIST (state, value) {
      state.sensorCheckList = value
    },
    CLEAR_SENSOR_CHECK (state, value) {
      state.sensorCheckList.push(value)
    },
    SET_DOWNLOAD_ID (state, value) {
      state.downloadId = value
    }
  },
  actions: {
    async getSensorList ({ commit }) {
      const res = await $http.get(`/monitor/realTimeAq/getCddyList?customerNo=${router.currentRoute.query.customerNo}`)
      commit('SET_SENSOR_LIST', res)
    }
  }
}
